package com.woniu.sharepark.common.service.impl;

import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.common.dao.LoginDAO;
import com.woniu.sharepark.common.dao.UserInfoDAO;
import com.woniu.sharepark.common.pojo.Login;
import com.woniu.sharepark.common.pojo.UserInfo;
import com.woniu.sharepark.common.service.UserService;
import com.woniu.sharepark.platformsystem.dao.VoucherDAO;
import com.woniu.sharepark.platformsystem.pojo.Voucher;
import com.woniu.sharepark.util.DesUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * @author:rhc
 * @date: 2019/6/5
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private LoginDAO loginDAO;
    @Resource
    private UserInfoDAO userInfoDAO;

    @Autowired
    private Voucher voucher;

    @Autowired
    private VoucherDAO voucherDAO;

    @Override
    public Login findUserByAccount(String account) {
        return loginDAO.selectByLoginName(account);
    }

    @Override
    public int saveLogin(Login login) {
        return loginDAO.insertSelective(login);
    }

    @Override
    public JsonResult register(Login login, String code, String rPassword, String invite) {
        Login newLogin = findUserByAccount(login.getlAccount());
        if (newLogin != null) {
            return JsonResult.error("账号已存在,请直接登陆!");
        }
        if (!login.getlPassword().equals(rPassword)) {
            return JsonResult.error("两次密码不一致,请重新输入密码");
        }
        if ("".equals(login.getrId()) || login.getrId() == null) {
            return JsonResult.error("请选择职位");
        }
        //将session验证码取出来
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String realCode = (String) session.getAttribute("code");
        if (!code.equals(realCode)) {
            return JsonResult.error("验证码不正确!");
        }
        //将数据保存到数据库
        try {
            login.setlPassword(new SimpleHash("MD5", login.getlPassword(), login.getlAccount(), 2).toString());
            saveLogin(login);
            //获取刚刚注册的用户ID
            int uid = findUserByAccount(login.getlAccount()).getlId();
            UserInfo userInfo = new UserInfo();
            userInfo.setlId(uid);
            userInfo.setuMoney(0.00);
            //将用户账号(手机号)添加进UserInfo @Zongjie.Li 2019.6.18
            userInfo.setuTel(login.getlAccount());
            userInfo.setuFlag(0);
            userInfoDAO.insert(userInfo);

            //以下为邀请码注册部分 @Zongjie.Li 2019.6.18
            if (invite == null || "".equals(invite)) {
                return JsonResult.ok("注册成功,请登陆");
            }
            System.out.println(invite);
            String idString = DesUtil.decryptBase64ToString(invite,"javaDesUtil");
            int inviteSendID = 0;
            try {
                inviteSendID = Integer.parseInt(idString);
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return JsonResult.ok("注册成功,但邀请码验证出错,无法获得奖励");
            }
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_WEEK,14);
            Date date = calendar.getTime();
            voucher.setuId(inviteSendID);
            //默认优惠券为5元
            voucher.setvSum(5.0);
            voucher.setvTerm(date);
            voucherDAO.insertSelective(voucher);
            voucher.setuId(userInfoDAO.selectUserInfoByLid(uid).getuId());
            voucherDAO.insertSelective(voucher);
            return JsonResult.ok("邀请注册成功,请登录");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("注册失败!");
        }
    }

    @Override
    public int resetPassword(Login login) {
        return loginDAO.updateByPrimaryKeySelective(login);
    }

    @Override
    public JsonResult addCommunity(Login login) {
        login.setrId(2);
        login.setlPassword(new SimpleHash("MD5", login.getlPassword(), login.getlAccount(), 2).toString());
        if (findUserByAccount(login.getlAccount()) != null) {
            return JsonResult.ok("账户已存在");
        }
        if (loginDAO.insertSelective(login) > 0) {
            return JsonResult.ok("添加物业成功");
        } else {
            return JsonResult.ok("添加物业失败");
        }
    }

    @Override
    @Cacheable(value = "userCount")
    public int findAllCount() {
        return loginDAO.selectAllCount();
    }

    @Override
    public JsonResult forgetPass(String phoneNum, String code, String newPass, String reNewPass) {
        if (loginDAO.selectByLoginName(phoneNum)==null){
            return JsonResult.error("账号不存在");
        }else{
            //将session验证码取出来
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String realCode = (String) session.getAttribute("code");
            if (!code.equals(realCode)) {
                return JsonResult.error("验证码不正确!");
            }
            String lPassword=new SimpleHash("MD5",newPass,phoneNum,2).toString();
            if (loginDAO.resetPass(phoneNum,lPassword)>0){
                return JsonResult.ok("重置密码成功");
            }else {
                return JsonResult.ok("重置密码失败");
            }
        }
    }
}
